<?php

namespace app\teacher\controller\grammar;

use app\common\controller\Frontend;
use app\common\model\banxi\grammar\GrammarGrade as GrammarGradeModel;
use app\common\model\banxi\grammar\GrammarSelectGrade;
use think\db\exception\DbException;

class GrammarGrade extends Frontend
{
    protected array $noNeedLogin = [''];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /*
     * 查询-语法年级
     * */
    public function listGrammarGrade()
    {
        $GrammarGradeModel = new GrammarGradeModel();
        $list = $GrammarGradeModel->order('sortNum', 'asc')
            ->order('id', 'desc')->select();

        $this->ok('操作成功', $list);
    }

    /*
     * 查询-当前选择年级
     * */
    public function getCurrentSelectGrade()
    {
        $userId = $this->request->get('userId', 0);
        $userId = $userId ? $userId : $this->auth->id;
        $GrammarSelectGrade = new GrammarSelectGrade();
        $selectGrade = $GrammarSelectGrade->getByStudentId($userId);
        if (!$selectGrade){
            $this->ok('操作成功');
        }

        $GrammarGradeModel = new GrammarGradeModel();
        $selectGrade = $GrammarGradeModel->where(['id' => $selectGrade['gradeId']])->find();

        $this->ok('操作成功', $selectGrade);
    }

    /*
     * 切换年级
     * */
    public function switchGrade()
    {
        $gradeId = $this->request->request('gradeId');
        $userId = $this->request->request('userId');

        $GrammarSelectGrade = new GrammarSelectGrade();
        $GrammarSelectGrade->switchGrade($userId ? $userId : $this->auth->id, $gradeId);

        $this->ok('操作成功');
    }

    /**
     * 分页查询
     * @return void
     * @throws DbException
     */
    public function listSelectPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query"]);
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $GrammarGradeModel = new GrammarGradeModel();
        $list = $GrammarGradeModel->getListSelectPage($where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }
}